<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/2/11
 * Time: 0:30
 */

$this->title = '小程序列表';
$web = \yii\helpers\Url::base();
?>


<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>

<!--search-->
<div class="panel panel-frame panel-operat">
    <div class="panel-body">
        <div class="panel-content">
            <input type="text" class="form-control search-box" placeholder="名称" id="name" name="name"
                   value="<?= $name ?>">
            <a href="javascript:search()" type="button" class="btn btn-warning search-icon">
                <div class="btn-icon"></div>
                <div class="btn-text">搜索</div>
            </a>
            <a href="javascript:editData()" type="button" class="btn btn-primary add-icon ">
                <div class="btn-icon"></div>
                <div class="btn-text">添加</div>
            </a>
        </div>
    </div>
</div>


<!--list-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <th>原始id</th>
                <th>APP_ID</th>
                <th>类型</th>
                <th>开通时间</th>
                <th>失效时间</th>
                <th>是否启用</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php
            for ($i = 0; $i < count($items); $i++) {
                $item = $items[$i];
                ?>
                <tr>
                    <td><?= $item['name'] ?></td>
                    <td><?= $item['raw_id'] ?></td>
                    <td><?= $item['app_id'] ?></td>
                    <td>
                        <?php
                        if ($item['type'] == 1) {
                            ?>
                            支付宝小程序
                            <?php
                        } elseif ($item['type'] == 2) {
                            ?>
                            微信小程序
                            <?php
                        }
                        ?>
                    </td>
                    <td><?= \app\librarys\Tools::getDatetimeFormat($item['begin_datetime'], 'Y-m-d') ?></td>
                    <td><?= \app\librarys\Tools::getDatetimeFormat($item['end_datetime'], 'Y-m-d') ?></td>
                    <td><span class="label label-<?= $item['is_enabled'] == 1 ? 'success' : 'danger' ?>">
                            <?= $item['is_enabled'] == 1 ? '已启用' : '已停用' ?>
                        </span></td>
                    <td><?= \app\librarys\Tools::getDatetimeFormat($item['create_datetime']) ?></td>


                    <td class="table-text-align-left">
                        <a class="btn btn-link" href="javascript:editData('<?= $item['id'] ?>')">编辑</a>
                        <a class="btn btn-link" href="javascript:deleteData('<?= $item['id'] ?>')">删除</a>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
    </div>
    <div class="table-pagination">
        <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
    </div>
</div>


<!--edit-->
<div class="modal fade" id="dataModalEdit" tabindex="-1" role="dialog"
     aria-labelledby="dataModalEditLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="dataModalEditLabel"><?= $this->title . '编辑' ?></h4>
            </div>
            <div id="modalEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">

    function search() {
        var name = $("#name").val();
        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (name != '' && name != null) {
            href_str = href_str + "&name=" + name;
        }
        window.location.href = href_str;
    }

    function editData(id) {

        $('#dataModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });

        $("#dataModalEdit").modal('show');
        $("#dataModalEdit").on('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }

        $("#dataModalEdit").find("#modalEdit").html('正在加载数据...');

        requestHtml('mini-program-partial', data, true, function (responseHtml) {
            $("#dataModalEdit").find("#modalEdit").html(responseHtml);
        });
    }

    function saveData() {
        var id = $("#data_form").find("#id").val();
        var name = $("#data_form").find("#name").val().trim();
        var raw_id = $("#data_form").find("#raw_id").val().trim();
        var app_id = $("#data_form").find("#app_id").val().trim();
        var app_secret = $("#data_form").find("#app_secret").val().trim();
        var type = $("#data_form").find("#type").val().trim();
        var begin_datetime = $("#data_form").find("#begin_datetime").val().trim();
        var end_datetime = $("#data_form").find("#end_datetime").val().trim();
        var is_enabled = 0;
        if ($("#data_form").find("#is_enabled").is(':checked')) {
            is_enabled = 1;
        }

        if (name == '' || name == null) {
            warningDialog('名称不能为空');
            return false;
        }

        if (raw_id == '' || raw_id == null) {
            warningDialog('原始id不能为空');
            return false;
        }

        if (app_id == '' || app_id == null) {
            warningDialog('APP_ID不能为空');
            return false;
        }

        if (app_secret == '' || app_secret == null) {
            warningDialog('密码不能为空');
            return false;
        }

        if (type == '' || type == null) {
            warningDialog('类型不能为空');
            return false;
        }

        if (begin_datetime == '' || begin_datetime == null) {
            warningDialog('开通时间不能为空');
            return false;
        }

        if (end_datetime == '' || end_datetime == null) {
            warningDialog('失效时间不能为空');
            return false;
        }

        var data = {};
        data.id = id;
        data.name = name;
        data.raw_id = raw_id;
        data.app_id = app_id;
        data.app_secret = app_secret;
        data.type = type;
        data.begin_datetime = begin_datetime;
        data.end_datetime = end_datetime;
        data.is_enabled = is_enabled;

        requestJson("mini-program-save-handler", data, true, function (response) {
            if (response.status == 0) {
                search();
            }
            else {
                errorDialog(response.message);
            }
        })
    }

    function deleteData(id) {
        confirmDialog(id, null, function (id) {
            var data = {};
            data.id = id;
            requestJson("mini-program-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    search();
                } else {
                    errorDialog(response.message);
                }
            });
        });
    }

</script>
